package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func RoleGroupSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectRoleGroupQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectRoleGroup(&selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func RoleGroupFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectRoleGroupQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindRoleGroup(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func RoleGroupCreate(ctx *gin.Context) {
	var addRoleGroup validate.AddRoleGroupWrapper

	err := ctx.ShouldBindJSON(&addRoleGroup)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateRoleGroup(addRoleGroup.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func RoleGroupUpdate(ctx *gin.Context) {
	var updateRoleGroup validate.UpdateRoleGroupWrapper

	err := ctx.ShouldBindJSON(&updateRoleGroup)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateRoleGroup(updateRoleGroup.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func RoleGroupDelete(ctx *gin.Context) {
	var deleteRoleGroupParam validate.DeleteRoleGroupParam

	err := ctx.ShouldBindJSON(&deleteRoleGroupParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deleteRoleGroupParam.Param, "sentence")

	service.DeleteRoleGroup(&selectQueryParam)

	// 查询成功
	response.Success(ctx, nil, "success")

}
